package net.googlecode.kharchenko.dao.impl;

import net.googlecode.kharchenko.dao.GroupDAO;
import net.googlecode.kharchenko.domains.Group;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * Realization of GroupDAO.
 */
@Repository
public class GroupDAOImpl implements GroupDAO {

    /**
     * Entity manager for database access.
     */
    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {

        this.entityManager = entityManager;
    }


    public List<Group> getAllGroups() {
        return (List<Group>) entityManager.createQuery("from Group").getResultList();

    }


    public Group getGroup(Long id) {


        return (Group) entityManager.createQuery("from Group  where id=?1")
                .setParameter(1, id).getSingleResult();

    }

}
